<template>
  <div class="app-main-container">
    <Breadcrumb :name="translate('skillActivity')" />
    <div class="warnningTip">
      <i class="el-icon-warning-outline" />
      {{ translate("skillHdPlace")
      }}<span class="primaryColor pointer underline" @click="handleMini">{{
        translate("miniFinish")
      }}</span
      >{{ translate("pageConfigDes") }}
    </div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clear"
    />
    <!-- <div v-if="meta.indexOf('CouponAdd') > -1" class="doumee-table-buttons"> -->
    <div class="doumee-table-buttons">
      <el-button type="primary" @click="handleEdit()">{{
        translate("add")
      }}</el-button>
      <el-button @click="handleSubscription()">{{
        translate("lookSubRecord")
      }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('hdname')"
        prop="name"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('goodsType')"
        min-width="80"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.businessType == '04'">{{
            translate("combo")
          }}</span>
          <span v-if="scope.row.businessType == '07'">{{
            translate("course")
          }}</span>
          <span v-if="scope.row.businessType == '19'">{{
            translate("zxk")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('hdsj')" width="180">
        <template slot-scope="scope">
          <div>{{ translate("rise") }}：{{ scope.row.startTime }}</div>
          <div>{{ translate("stop") }}：{{ scope.row.endTime }}</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('hdStatus')"
        prop=""
        show-overflow-tooltip
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status == '0'">{{
            translate("nostarted")
          }}</span>
          <span v-if="scope.row.status == '1'">{{
            translate("underway")
          }}</span>
          <span v-if="scope.row.status == '2'">{{ translate("over") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('hdjg')"
        prop="discountPrice"
        show-overflow-tooltip
        min-width="80"
      />
      <el-table-column
        align="center"
        :label="translate('hdOrderNums')"
        prop=""
        show-overflow-tooltip
        min-width="100"
      >
        <template v-slot="scope">
          <span class="primaryColor pointer" @click="orderClick(scope.row)">{{
            scope.row.sale_count
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('inventory')"
        prop="surplus"
        show-overflow-tooltip
        min-width="80"
      />
      <el-table-column
        align="center"
        :label="translate('bz')"
        prop="remark"
        show-overflow-tooltip
        min-width="120"
      />
      <el-table-column
        :label="translate('handle')"
        min-width="150"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- v-if="meta.indexOf('CouponDel') > -1" -->
          <div class="flex-cc">
            <el-button
              v-if="scope.row.status == '0' || scope.row.status == '1'"
              v-preventReClick
              type="text"
              @click="handleEdit(scope.row)"
              >{{ translate("edit") }}</el-button
            >
            <el-button
              v-if="scope.row.status == '0' || scope.row.status == '1'"
              v-preventReClick
              type="text"
              @click="handlePreview(scope.row)"
              >{{ translate("hb") }}</el-button
            >
            <el-button
              v-if="scope.row.status == '1'"
              v-preventReClick
              type="text"
              style="color: red"
              @click="toEnd(scope.row.id)"
              >{{ translate("end") }}</el-button
            >
            <el-button
              v-if="scope.row.status == '0' || scope.row.status == '2'"
              v-preventReClick
              type="text"
              style="color: red"
              @click="toDelete(scope.row.id)"
              >{{ translate("del") }}</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <el-dialog
      :title="translate('posterDown')"
      :visible.sync="isShowAvatar"
      width="600px"
    >
      <div>{{ translate("posterDownDesc") }}。</div>
      <div style="text-align: center" v-if="activeRow.poster">
        <el-image
          style="width: 300px"
          :src="activeRow.poster"
          :preview-src-list="[activeRow.poster]"
        />
      </div>
      <template v-else>
        <div
          style="
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
          "
        >
          <el-icon class="is-loading" style="font-size: 24px; color: #409eff">
            <Loading />
          </el-icon>
          <span style="margin-left: 8px; color: #409eff">加载中...</span>
        </div>
      </template>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleDown('0')">{{
          translate("qrcodeDown")
        }}</el-button>
        <el-button type="primary" @click="handleDown('0')">{{
          translate("downPoster")
        }}</el-button>
      </span>
    </el-dialog>
    <Edit
      v-if="isShowEdit"
      ref="EditRef"
      @success="getList"
      @close="isShowEdit = false"
    />
    <Detail ref="DetailRef" />
  </div>
</template>

<script>
import {
  SecKillList,
  venueList,
  SecKillDel,
  SecKillEnd,
  SecKillAvatar,
} from "@/api";
import Pagination from "@/components/paginations";
import { queryFormConfig } from "./config";
import Edit from "./edit.vue";
import Detail from "./detail.vue";
import Breadcrumb from "../components/Breadcrumb.vue";
export default {
  name: "SeckillActive",
  components: {
    Pagination,
    Edit,
    Breadcrumb,
    Detail,
  },
  data() {
    return {
      isShowEdit: false,
      isShowAvatar: false,
      visible: false,
      queryFormConfig,
      totalCount: 0,
      list: [],
      activeRow: {},
      meta: [],
      listLoading: true,
      venueList: [],
      orderType: [],
      pagination: {
        page: 1,
        rows: 10,
      },
      suitableVenue: [],
      filters: {},
    };
  },
  created() {
    this.meta = this.$route.meta.buttons || [];
    this.getVenueList();
    // this.getOrderTypeList()
    this.getList(1);
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout();
    });
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value;
      this.getList(1);
    },
    handleEdit(row) {
      console.log("row", row);
      this.isShowEdit = true;
      this.$nextTick(() => {
        if (row && row.id) {
          this.$refs.EditRef.getDetail(row);
        }
        this.$refs.EditRef.isShowModal = true;
      });
    },
    backApplication() {
      this.$router.push("/application/applicationCenter");
    },
    handleSubscription() {
      this.$router.push({
        name: "SecKillMemberList",
      });
    },
    orderClick(row) {
      this.$router.push({
        name: "SecKillOrderList",
        query: {
          id: row.id,
        },
      });
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {},
      })
        .then((res) => {
          if (res.errorCode !== "000000") return;
          // this.venueList = res.recordList
          const arr = res.recordList;
          arr.forEach((e) => {
            e.label = e.name;
            e.value = e.id;
          });
          this.queryFormConfig.formItems[3].options = arr;
        })
        .catch(() => {});
    },

    getList(page) {
      this.listLoading = true;
      const { filters, pagination } = this;
      if (page) {
        pagination.page = page;
        this.pagination.page = page;
      }
      SecKillList({
        pagination,
        param: { ...filters, type: "0" },
      })
        .then((res) => {
          this.listLoading = false;
          if (res.errorCode === "000000") {
            this.list = res.recordList;
            this.totalCount = res.totalCount;
          }
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    // 删除
    toDelete(id) {
      const that = this;
      this.$confirm(this.translate("delSkillTip"), this.translate("ts"), {
        confirmButtonText: this.translate("qd"),
        cancelButtonText: this.translate("cancel"),
        type: "warning",
      })
        .then((res) => {
          SecKillDel({ param: { id } }).then((res) => {
            if (res.errorCode !== "000000") return;
            that.$message.success(this.translate("delScs"));
            that.getList();
          });
        })
        .catch(() => {
          // this.$message.info(this.translate('canceledDel'))
        });
    },
    handlePreview(row) {
      this.activeRow = {};
      this.isShowAvatar = true;
      SecKillAvatar({ param: { id: row.id } }).then((res) => {
        this.activeRow = res.record || "";
      });
    },
    handleDown(str) {
      if (str === "0") {
        window.open(this.activeRow.poster, "_blank");
      } else {
        window.open(this.activeRow.ewm, "_blank");
      }
      this.isShowAvatar = false;
    },
    toEnd(id) {
      const that = this;
      this.$confirm(this.translate("endSkillTip"), this.translate("ts"), {
        confirmButtonText: this.translate("qd"),
        cancelButtonText: this.translate("cancel"),
        type: "warning",
      })
        .then((res) => {
          SecKillEnd({ param: { id } }).then((res) => {
            if (res.errorCode !== "000000") return;
            that.$message.success(this.translate("endScs"));
            that.getList();
          });
        })
        .catch(() => {
          // this.$message.info(this.translate('canceledDel'))
        });
    },
    // 清空
    clear() {
      this.filters = {};
      this.getList(1);
    },
    handleMini() {
      this.$router.push("/application/miniFinish");
    },
  },
};
</script>
<style lang="scss">
/* 旋转动画 */
.loading-icon {
  animation: rotating 1.5s linear infinite;
  font-size: 32px;
  color: #409eff;
}

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 加载状态容器 */
.loading-box {
  height: 200px; /* 根据图片高度设置 */
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
}

.error-icon {
  font-size: 32px;
  color: #f56c6c;
}
</style>
